package com.wish.basic.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.basic.entity.*;
import com.wish.basic.service.*;
import com.wish.dto.login.UserDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyk
 * @since 2022-08-18
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /*@Value("${jwt.key}")//取配置文件里的jwt.key
    private String jwtKey;*/

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IRoleMenuService roleMenuService;

    @Autowired
    private IMenuService menuService;


    @GetMapping("/getByUsername")
    public UserDto getByUsername(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userService.getOne(queryWrapper);

        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user,userDto);

        //get roleid
        QueryWrapper<UserRole> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id", userDto.getId());
        List<UserRole> userRoleList = userRoleService.list(queryWrapper1);
        List<Integer> roleIds = userRoleList.stream().map(userRole -> userRole.getRoleId()).collect(Collectors.toList());
        //get role
        QueryWrapper<RoleMenu> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("role_id",roleIds);
        List<RoleMenu> roleMenuList = roleMenuService.list(queryWrapper2);
        List<Integer> menuIds = roleMenuList.stream().map(roleMenu -> roleMenu.getMenuId()).collect(Collectors.toList());

        //批量查询菜单
        QueryWrapper<Menu> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.in("id",menuIds);
        List<Menu> menuList = menuService.list(queryWrapper3);
        Set<String> authCodeSet = menuList.stream().map(menu -> menu.getAuthCode()).collect(Collectors.toSet());
        userDto.setAuthCodeSet(authCodeSet);
        return userDto;
    }


   /* @RequestMapping("/login")
    public JsonResult login(@RequestBody User user){
        Map<String,Object> map=new HashMap<>();
        //查询用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        User loginUser = userService.getOne(wrapper);
        if(null==loginUser){

            return  JsonResult.faild("用户不存在");
        }
        if(!loginUser.getPassword().equals(user.getPassword())){
            return  JsonResult.faild("密码错误");
        }
        //登录成功
        String token = JWT.create()
                .setKey(jwtKey.getBytes())
                .setPayload("username", loginUser.getUsername())
                .setExpiresAt(DateUtil.offsetDay(DateUtil.date(), 1))
                .sign();
        return JsonResult.successd(token);

    }*/
}
